import { createRouter, createWebHashHistory, RouteLocationNormalized, RouteRecordRaw } from 'vue-router'
import { jilurizhi } from '../api'
import Home from './views/Home.vue'
import index from './views/index.vue'
import Login from './views/login.vue'
import teamfile from './views/team/oss.vue'

function toID(rote: RouteLocationNormalized): Record<string, any> {
  return {
    ID: Number(rote.params.ID)
  }

}

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: '主页',
    component: index,
    // 主页默认进入首页
    redirect: to => {
      return { name: '团队文件' }
    },
    children: [
      {
        path: 'home',
        name: '首页',
        component: Home
      },
      {
        path: 'teamfile',
        name: '团队文件',
        component: teamfile
      }
    ]
  },
  {
    path: '/login',
    name: '登录',
    component: Login
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach(async (to, from, next) => {
  // 登录者信息
  // const online = sessionStorage.getItem('online')
  // try {
  //   if (to.path !== '/login') {
  //     const onlineRet = await online()
  //   }
  next()
  // } catch (error) {
  //   console.error("beforeEach onlineRet error=>", error)
  //   next('/login')
  // }
})

router.afterEach((to, from) => {
  const arr: [string, string][] = []
  arr[0] = ['type', 'router']
  arr[1] = ['typename1', to.name ? String(to.name) : '']
  arr[2] = ['__from__', from.name ? String(from.name) : '']
  jilurizhi(arr)
})

export default router
